using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;

using Gizmox.WebGUI.Forms;
using Gizmox.WebGUI.Forms.Dialogs;
using Gizmox.WebGUI.Common.Resources;

namespace SFE.Categories.DataControls
{
	/// <summary>
	/// Summary description for ListViewControl.
	/// </summary>

    [Serializable()]
    public class ListViewControl : UserControl, IHostedApplication
	{
		private Gizmox.WebGUI.Forms.ListView mobjListView;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnFrom;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnSubject;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnReceived;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnSize;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnImportant;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnOpened;
		private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnAttached;
        private Gizmox.WebGUI.Forms.ColumnHeader mobjColumnControl;

		private RandomData mobjRandomData = null;

		/// <summary> 
		/// Required designer variable.
		/// </summary>
        [NonSerialized]
        private System.ComponentModel.Container components = null;

		public ListViewControl()
		{
			// This call is required by the WebGUI Form Designer.
			InitializeComponent();

			mobjRandomData = new RandomData();

			for(int i=0;i<60;i++)
			{
				AddItem();
			}

			//this.mobjListView.ListViewItemSorter = new ListViewItemSorter(this.mobjListView);

            foreach (ColumnHeader objCol in this.mobjListView.Columns)
            {
                if (objCol.Type != ListViewColumnType.Control)
                {
                    objCol.AutoResize(ColumnHeaderAutoResizeStyle.ColumnContent);
                }
            }

			// Check events
			//this.mobjListView.SelectedIndexChanged+=new EventHandler(mobjListView_SelectedIndexChanged);
			//this.mobjListView.DoubleClick+=new EventHandler(mobjListView_DoubleClick);
		}



		private void mobjListView_SelectedIndexChanged(object sender, EventArgs e)
		{
			MessageBox.Show("mobjListView_SelectedIndexChanged");
		}

		private void mobjListView_DoubleClick(object sender, EventArgs e)
		{
			MessageBox.Show("mobjListView_DoubleClick");
		}

		/// <summary> 
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		private void AddItem()
		{
			ListViewItem objItem = null;

            ListViewControlPanel objPanel = new ListViewControlPanel();
            objPanel.Visible = false;

			if(mobjRandomData.GetBoolean())
			{
                objItem = this.mobjListView.Items.Add(objPanel, GetIcon("ImportantMail.gif"));
			}
			else
			{
                objItem = this.mobjListView.Items.Add(objPanel, "");
			}

			if(mobjRandomData.GetBoolean())
			{
				objItem.SubItems.Add(GetIcon("OpenedMail.gif"));                
                objPanel.Read = true;
			}
			else
			{
				objItem.SubItems.Add(GetIcon("ClosedMail.gif"));
                objPanel.Read = false;
			}
			if(mobjRandomData.GetBoolean())
			{
				objItem.SubItems.Add(GetIcon("AttachedMail.gif"));
                objPanel.Attachemnts = true;
			}
			else
			{
				objItem.SubItems.Add("");
                objPanel.Attachemnts = false;
			}
			objItem.SubItems.Add(objPanel.Subject = "This is a test message.");
            objItem.SubItems.Add(objPanel.From = "test@visualwebgui.com");
			
			objItem.SubItems.Add(mobjRandomData.GetDate().ToString());
            objItem.SubItems.Add(objPanel.MailSize = mobjRandomData.GetSize());

            Button objButton = new Button();
            objButton.Click += new EventHandler(OnEditButtonClick);
            objButton.Tag = objItem;
            objButton.Text = "Edit";
            objItem.SubItems.Add(objButton);


            if (mobjRandomData.GetBoolean())
            {
                objItem.UseItemStyleForSubItems = false;
                objItem.SubItems[2].BackColor = Color.Yellow;
            }

            if (mobjRandomData.GetBoolean())
            {
                objItem.UseItemStyleForSubItems = false;
                objItem.SubItems[4].BackColor = Color.LightGreen;
            }



		}

        private void OnEditButtonClick(object sender, EventArgs e)
        {
            Button objButton = sender as Button;
            if (objButton != null)
            {
                ListViewPanelItem objPanelItem = objButton.Tag as ListViewPanelItem;
                if (objPanelItem != null)
                {
                    if (objPanelItem.Panel.Visible = !objPanelItem.Panel.Visible)
                    {
                        objButton.Text = "Commit";

                        
                    }
                    else
                    {
                        objButton.Text = "Edit";

                        ListViewControlPanel objPanel = objPanelItem.Panel as ListViewControlPanel;
                        if (objPanel != null)
                        {

                            if (objPanel.Important)
                            {
                                objPanelItem.SubItems[0].Text = GetIcon("ImportantMail.gif");
                            }
                            else
                            {
                                objPanelItem.SubItems[0].Text = "";
                            }

                            if (objPanel.Read)
                            {
                                objPanelItem.SubItems[1].Text = GetIcon("OpenedMail.gif");
                            }
                            else
                            {
                                objPanelItem.SubItems[1].Text = GetIcon("ClosedMail.gif");
                            }

                            if (objPanel.Attachemnts)
                            {
                                objPanelItem.SubItems[2].Text = GetIcon("AttachedMail.gif");
                            }
                            else
                            {
                                objPanelItem.SubItems[2].Text = "";
                            }

                            objPanelItem.SubItems[3].Text = objPanel.Subject;
                            objPanelItem.SubItems[4].Text = objPanel.From;

                            objPanelItem.SubItems[6].Text = objPanel.MailSize;

                        }
                    }
                }
            }
        }

		private string GetIcon(string strName)
		{
			return (new IconResourceHandle(strName)).ToString();
		}

		#region Component Designer generated code
		/// <summary> 
		/// Required method for Designer support - do not modify 
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ListViewControl));
            this.mobjListView = new Gizmox.WebGUI.Forms.ListView();
            this.mobjColumnImportant = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnOpened = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnAttached = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnSubject = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnFrom = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnReceived = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnSize = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.mobjColumnControl = new Gizmox.WebGUI.Forms.ColumnHeader();
            this.SuspendLayout();
            // 
            // mobjListView
            // 
            this.mobjListView.AutoGenerateColumns = true;
            this.mobjListView.BackgroundImageLayout = Gizmox.WebGUI.Forms.ImageLayout.None;
            this.mobjListView.Columns.AddRange(new Gizmox.WebGUI.Forms.ColumnHeader[] {
            this.mobjColumnImportant,
            this.mobjColumnOpened,
            this.mobjColumnAttached,
            this.mobjColumnSubject,
            this.mobjColumnFrom,
            this.mobjColumnReceived,
            this.mobjColumnSize,
            this.mobjColumnControl});
            this.mobjListView.DataMember = null;
            this.mobjListView.Dock = Gizmox.WebGUI.Forms.DockStyle.Fill;
            this.mobjListView.ItemsPerPage = 20;
            this.mobjListView.Location = new System.Drawing.Point(0, 0);
            this.mobjListView.MultiSelect = false;
            this.mobjListView.Name = "mobjListView";
            this.mobjListView.ShowItemToolTips = false;
            this.mobjListView.Size = new System.Drawing.Size(578, 578);
            this.mobjListView.TabIndex = 0;
            this.mobjListView.UseInternalPaging = true;
            // 
            // mobjColumnImportant
            // 
            this.mobjColumnImportant.Image = new Gizmox.WebGUI.Common.Resources.IconResourceHandle(resources.GetString("mobjColumnImportant.Image"));
            this.mobjColumnImportant.Text = "";
            this.mobjColumnImportant.Type = Gizmox.WebGUI.Forms.ListViewColumnType.Icon;
            this.mobjColumnImportant.Width = 20;
            // 
            // mobjColumnOpened
            // 
            this.mobjColumnOpened.Image = new Gizmox.WebGUI.Common.Resources.IconResourceHandle(resources.GetString("mobjColumnOpened.Image"));
            this.mobjColumnOpened.Text = "";
            this.mobjColumnOpened.Type = Gizmox.WebGUI.Forms.ListViewColumnType.Icon;
            this.mobjColumnOpened.Width = 20;
            // 
            // mobjColumnAttached
            // 
            this.mobjColumnAttached.Image = new Gizmox.WebGUI.Common.Resources.IconResourceHandle(resources.GetString("mobjColumnAttached.Image"));
            this.mobjColumnAttached.Text = "Action";
            this.mobjColumnAttached.Type = Gizmox.WebGUI.Forms.ListViewColumnType.Icon;
            this.mobjColumnAttached.Width = 20;
            // 
            // mobjColumnSubject
            // 
            this.mobjColumnSubject.Image = null;
            this.mobjColumnSubject.Text = "Subject";
            this.mobjColumnSubject.Width = 250;
            // 
            // mobjColumnFrom
            // 
            this.mobjColumnFrom.Image = null;
            this.mobjColumnFrom.Text = "From";
            this.mobjColumnFrom.Width = 150;
            // 
            // mobjColumnReceived
            // 
            this.mobjColumnReceived.Image = null;
            this.mobjColumnReceived.Text = "Received";
            this.mobjColumnReceived.Width = 150;
            // 
            // mobjColumnSize
            // 
            this.mobjColumnSize.Image = null;
            this.mobjColumnSize.Text = "Size";
            this.mobjColumnSize.Width = 50;
            // 
            // mobjColumnControl
            // 
            this.mobjColumnControl.Image = null;
            this.mobjColumnControl.PreferedItemHeight = 23;
            this.mobjColumnControl.Text = "";
            this.mobjColumnControl.Type = Gizmox.WebGUI.Forms.ListViewColumnType.Control;
            this.mobjColumnControl.Width = 200;
            // 
            // ListViewControl
            // 
            this.Controls.Add(this.mobjListView);
            this.DockPadding.All = 3;
            this.Padding = new Gizmox.WebGUI.Forms.Padding(3);
            this.Size = new System.Drawing.Size(584, 528);
            this.ResumeLayout(false);

		}
		#endregion

		#region IHostedApplication Members

		public void InitializeApplication()
		{
		}
        public string GetTitle()
        {
            return "Category";
        }
		public HostedToolBarElement[] GetToolBarElements()
		{
			ArrayList objElements = new ArrayList();
			objElements.Add(new HostedToolBarButton("Columns",new IconResourceHandle("Columns.gif"),"Columns"));
			objElements.Add(new HostedToolBarButton("Sorting",new IconResourceHandle("Sorting.gif"),"Sorting"));
			objElements.Add(new HostedToolBarSeperator());
			objElements.Add(new HostedToolBarToggleButton("CheckBoxes",new IconResourceHandle("CheckBoxes.gif"),"CheckBoxes"));
			objElements.Add(new HostedToolBarToggleButton("MultiSelect",new IconResourceHandle("MultiSelect.gif"),"MultiSelect"));
			objElements.Add(new HostedToolBarSeperator());
			objElements.Add(new HostedToolBarButton("Add Item",new IconResourceHandle("NewItem.gif"),"AddItem"));
			objElements.Add(new HostedToolBarButton("Remove Item",new IconResourceHandle("RemoveItem.gif"),"RemoveItem"));
			objElements.Add(new HostedToolBarButton("Clear Items",new IconResourceHandle("Clear.gif"),"ClearItems"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton("Properties", new IconResourceHandle("Properties.gif"), "Properties"));
            objElements.Add(new HostedToolBarSeperator());
            objElements.Add(new HostedToolBarButton("Help", new IconResourceHandle("Help.gif"), "Help"));
			return (HostedToolBarElement[])objElements.ToArray(typeof(HostedToolBarElement));
		}

		public void OnToolBarButtonClick(HostedToolBarButton objButton, EventArgs objEvent)
		{
			HostedToolBarToggleButton objHostedToolBarToggleButton = null;

			string strAction = (string)objButton.Tag;
			switch(strAction)
			{
				case "AddItem":
					AddItem();
					break;
				case "RemoveItem":
					if(this.mobjListView.Items.Count>0)
					{
                        if (this.mobjListView.SelectedItem != null)
                        {
                            this.mobjListView.SelectedItem.Remove();
                        }
					}
					break;
				case "ClearItems":
					this.mobjListView.Items.Clear();
					break;
				case "Columns":
					ListViewColumnOptions objListViewColumnOptions = new ListViewColumnOptions(this.mobjListView);
					objListViewColumnOptions.ShowDialog();
					break;
				case "Sorting":
					ListViewSortingOptions objListViewSortingOptions = new ListViewSortingOptions(this.mobjListView);
					objListViewSortingOptions.ShowDialog();
					break;
				case "CheckBoxes":
					objHostedToolBarToggleButton = objButton as HostedToolBarToggleButton;
					if(objHostedToolBarToggleButton!=null)
					{
						this.mobjListView.CheckBoxes = objHostedToolBarToggleButton.Pushed;
					}
					break;
				case "MultiSelect":
					objHostedToolBarToggleButton = objButton as HostedToolBarToggleButton;
					if(objHostedToolBarToggleButton!=null)
					{
						this.mobjListView.MultiSelect = objHostedToolBarToggleButton.Pushed;
					}
					break;
                case "Properties":
                    InspectorForm objInspectorForm = new InspectorForm();
                    objInspectorForm.SetControls(this.mobjListView);
                    objInspectorForm.ShowDialog();
                    break;
                case "Help":
                    Help.ShowHelp(this, CatalogSettings.ProjectCHM, HelpNavigator.KeywordIndex, "ListView class");
                    break;
			}
		}

		#endregion


	}
}
